package site.fantasycube.blog.controller;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.fantasycube.blog.common.enums.CodeEnum;
import site.fantasycube.blog.common.enums.RoleEnums;
import site.fantasycube.blog.common.enums.UserStatusEnum;
import site.fantasycube.blog.domain.BlogUser;
import site.fantasycube.blog.service.IUserService;
import site.fantasycube.blog.util.ParamUtils;
import site.fantasycube.blog.util.redis.IRedisCache;
import site.fantasycube.blog.util.result.BaseResult;
import site.fantasycube.blog.util.result.Fail;
import site.fantasycube.blog.util.result.Success;
import site.fantasycube.blog.vo.ForgetVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author 幻&乐
 * @Date 2021/12/25
 * 描述 用户前端控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private IUserService userService;
    private IRedisCache redisCache;

    @Autowired
    public void setRedisCache(IRedisCache redisCache) {
        this.redisCache = redisCache;
    }

    @Autowired
    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    /**
     * 描述： 注册接口
     *
     * @param entity{username,password}
     * @return BaseResult 操作结果
     * @author 幻&乐
     * @Date 2021/12/26 2:35
     **/
    @PostMapping("/register.out")
    public BaseResult add(@RequestBody ForgetVo entity, HttpServletRequest request) {
        //校验id必须为空
        ParamUtils.isNull(entity.getId());
        //校验username和password不能为空
        ParamUtils.nonNull(entity.getUsername(), "输入用户名呀！");
        ParamUtils.nonNull(entity.getPassword(), "密码都不输入，我怎么知道是你？");
        if (entity.getPassword().equals(entity.getForgetPassword())){
            return Fail.me(CodeEnum.CODE_NOT_ACCEPT.getCode(),"两次密码不一致啊！");
        }
        //--------------解密预留
        BlogUser user = new BlogUser();
        user.setUsername(entity.getUsername());
        user.setPassword(entity.getPassword());
        user.setAddDate(new Date());
        //--------------
        user.setRole(RoleEnums.BLOG_USER.getCode());
        user.setAddDate(new Date());
        BlogUser add = userService.registration(user);
        BaseResult me = Success.me("注册成功").setData(new ForgetVo(user.getId(),user.getUsername()));
        if (ObjectUtil.isEmpty(add)) {
            me = Fail.me(CodeEnum.CODE_CONTENT_ERROR.getCode(), "内部错误，请重新尝试，再次尝试错误请联系管理员！");
        }
        return me;
    }

    /**
     * @param entry{username,password}
     * @return BaseResult 操作结果
     * @author 幻&乐
     * @Date 2021/12/26
     * 描述 登陆接口
     */
    @PostMapping("/login.out")
    public BaseResult login(@RequestBody BlogUser entry) {
        ParamUtils.nonNull(entry.getPassword(), "输入密码呀！");
        BlogUser dbUser = null;
        //校验是否为邮箱登陆
        if (!ObjectUtil.isNotNull(entry.getEmail())) {
            ParamUtils.nonNull(entry.getUsername(), "输入用户名啊！（震声）");
            dbUser = userService.getByUserName(entry.getUsername());
        }else {
            dbUser = userService.getByEmail(entry.getEmail());
        }
        ParamUtils.nonNull(dbUser, "用户不存在啊！");
        if (!entry.getPassword().equals(dbUser.getPassword())) {
            return Fail.me(CodeEnum.CODE_NOT_ACCEPT.getCode(), "密码不对啊？");
        }
        if (dbUser.getStatus().equals(UserStatusEnum.USER_STATUS_NOASSWSS.getCode())) {
            return Fail.me(CodeEnum.CODE_NO_ACCESS.getCode(), CodeEnum.CODE_NO_ACCESS.getMessage());
        }
        if (dbUser.getStatus().equals(UserStatusEnum.USER_STATUS_FREEZE)) {
            return Fail.me(CodeEnum.CODE_UNAUTHORIZED.getCode(), "账号已冻结暂停访问");
        }
        return Success.me("登陆成功");
    }


    /**
     * @param vo {email,forgetCode}
     * @author 幻&乐
     * @Date 2021/12/26
     * 描述 找回密码
     */
    @PostMapping("/passwordForget")
    public BaseResult passwordForget(@RequestBody ForgetVo vo) {
        ParamUtils.nonNull(vo.getForgetCode(), "验证码不能为空");
        ParamUtils.nonNull(vo.getEmail(), "邮箱不能为空");
        ParamUtils.nonNull(vo.getPassword(),"密码不能为空");
        if (!vo.getPassword().equals(vo.getForgetPassword())){
            Fail.me(CodeEnum.CODE_NO_ACCESS.getCode(),"两次密码不一致");
        }
        //验证code是否过期
        if (redisCache.chackMsg(vo.getForgetCode())){
            Fail.me(CodeEnum.CODE_NO_ACCESS.getCode(),"验证码已过期");
        }
        BlogUser dbUser = userService.getByEmail(vo.getEmail());
        if (ObjectUtil.isNotNull(dbUser) && dbUser.getStatus().equals(UserStatusEnum.USER_STATUS_NOEMAIL.getCode())) {
            Fail.me(CodeEnum.CODE_CONTENT_ERROR.getCode(), "账号未绑定邮箱");
        }
        dbUser.setPassword(vo.getPassword());
        boolean update = userService.updateById(dbUser);
        if (!update){
            Fail.me(CodeEnum.CODE_CONTENT_ERROR.getCode(),"重置失败，请联系管理员！");
        }
        return Success.me("密码重置成功");
    }

    /**
     * 描述： 修改用户名
     *
     * @param entry{id,username}
     * @return BaseResult 操作结果
     * @author 幻&乐
     * @Date 2021/12/26 2:43
     **/
    @PostMapping("/userForget")
    public BaseResult userForget(@RequestBody BlogUser entry) {
        return Success.me("用户名修改成功");
    }

    /**
     * 描述： 发送验证码
     *
     * @param entry {email}
     * @author 幻&乐
     * @Date 2021/12/26 2:32
     **/
    @PostMapping("/sendMsg")
    public BaseResult sendMsg(@RequestBody BlogUser entry) {
        if (entry.getStatus().equals(UserStatusEnum.USER_STATUS_NOEMAIL.getCode())) {
            Fail.me(CodeEnum.CODE_CONTENT_ERROR.getCode(), "账号未绑定邮箱");
        }
        return Success.me("发送成功");
    }

    /**
     * 描述： 绑定邮箱
     *
     * @param vo {email,code}
     * @return BaseResult
     * @author 幻&乐
     * @Date 2021/12/26 2:55
     **/
    @PostMapping("/bindEmail")
    public BaseResult bindEmail(@RequestBody ForgetVo vo) {
        return Success.me("绑定成功");
    }
}
